<?php
$blog='NULL';
$blogdata='NULL';

define('WP_INSTALLING', true);

if ( !(phpversion() >= '4.3') )
	die( 'Your server is running PHP version ' . phpversion() . ' but Lyceum requires at least 4.3' );

if ( !extension_loaded('mysql') )
	die( 'Your PHP installation appears to be missing the MySQL which is required for WordPress.' );

if (!file_exists('../config/wp-config.php') && !file_exists('../../config/wp-config.php')) 
	die("There doesn't seem to be a <code>wp-config.php</code> file. I need this before we can get started. Need more help? <a href='http://wordpress.org/docs/faq/#wp-config'>We got it</a>. You can <a href='setup-config.php'>create a <code>wp-config.php</code> file through a web interface</a>, but this doesn't work for all server setups. The safest way is to manually create the file.");
require('../private.php');
require_once(CONFIGPATH . 'wp-config.php');
require_once(WPADMININC . 'upgrade-functions.php');
require_once(PRIVATEROOT . 'wp-includes/registration-functions.php');
require_once(ABSPATH . '/wp-admin/upgrade-schema.php');

$writablethings = array( '../', '../wp-content/blogs/' );
if (USELOGS)
	$writablethings[] = LOGDIR;
if (!SAVEWPCOMMENTS)
	$writablethings[] = '../wp-comments-post.php';
	
foreach ($writablethings as $thing)
	if ( !is_writable($thing) )
		die( "<strong>".realpath($thing)." </strong> must be writable by the webserver." );

//mkdir($logdirectory .'/lyceum');//default is 777. would be nice to use a more restricted mode, but i don't think this is possible for systems running httpd as nobody

$schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
$guessurl = str_replace('/wp-admin/install.php?step=2', '', $schema . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) );
$siteurl = preg_replace('|/wp-admin/.*|i', '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']) . '/';

if (isset($_GET['step']))
	$step = $_GET['step'];
else
	$step = 0;
header( 'Content-Type: text/html; charset=utf-8' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><?php _e('Lyceum &rsaquo; Installation'); ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<style media="screen" type="text/css">
	<!--
	html {
		background: #eee;
	}
	body {
		background: #fff;
		color: #000;
		font-family: Georgia, "Times New Roman", Times, serif;
		margin-left: 20%;
		margin-right: 20%;
		padding: .2em 2em;
	}
	
	h1 {
		color: #006;
		font-size: 18px;
		font-weight: lighter;
	}
	
	h2 {
		font-size: 16px;
	}
	
	p, li, dt {
		line-height: 140%;
		padding-bottom: 2px;
	}

	ul, ol {
		padding: 5px 5px 5px 20px;
	}
	#logo {
		margin-bottom: 2em;
		text-align: center;
	}
	.step a, .step input {
		font-size: 2em;
	}
	td input {
		font-size: 1.5em;
	}
	.step, th {
		text-align: right;
	}
	#footer {
		text-align: center; 
		border-top: 1px solid #ccc; 
		padding-top: 1em; 
		font-style: italic;
	}
	-->
	</style>
</head>
<body>
<h1 id="logo"><img alt="Lyceum" src="images/lyceum-logo.png" /></h1>
<?php
// Let's check to make sure WP isn't already installed.
if (!LYCEUMDEBUG){
$wpdb->hide_errors();
$installed = $wpdb->get_results("SELECT * FROM $wpdb->users");
if ($installed) die('<h1>'.__('Already Installed').'</h1><p>'.__('You appear to have already installed Lyceum. To reinstall please clear your old database tables first.').'</p></body></html>');
$wpdb->show_errors();
}

switch($step) {

	case 0:
?>
<p><?php printf(__('Welcome to Lyceum installation. We&#8217;re now going to go through a few steps to get you up and running with the latest in personal publishing platforms. You may want to peruse the <a href="%s">ReadMe documentation</a> at your leisure. WARNING: by going to the next page you will delete your current installation'), '../readme.html'); ?></p>
	<h2 class="step"><a href="install.php?step=1"><?php _e('First Step &raquo;'); ?></a></h2>
<?php
	break;

	case 1:
	if (WEBREINSTALL){
		$sql = "DROP TABLE IF EXISTS $wpdb->blogs, $wpdb->categories, $wpdb->comments, $wpdb->linkcategories, $wpdb->links, $wpdb->options, $wpdb->post2cat, $wpdb->postmeta, $wpdb->posts, $wpdb->usermeta, $wpdb->users, $wpdb->postsearch, $wpdb->sessions";
		$wpdb->query($sql);
		empty_dir('../wp-content/blogs/');
	}
?>
<h1><?php _e('First Step'); ?></h1>
<p><?php _e("Before we begin we need a little bit of information. Don't worry, you can always change these later."); ?></p>

<form id="setup" method="post" action="install.php?step=2"><?php echo bloginput()?>
<table width="100%">
<tr>
<th width="33%"><?php _e('Site title:'); ?></th>
<td><input name="site_title" type="text" id="site_title" size="25"<?php echo  defined('SITETITLE') ? ' value="'.SITETITLE.'"' : "" ?>/></td>
</tr>
<tr>
<th><?php _e('Admin e-mail:'); ?></th>
	<td><input name="admin_email" type="text" id="admin_email" size="25"<?php echo  defined('ADMINEMAIL') ? ' value="'.ADMINEMAIL.'"' : "" ?>/></td>
</tr>
</table>
<p><em><?php _e('Double-check that email address before continuing.'); ?></em></p>
<h2 class="step">
<input type="submit" name="Submit" value="<?php _e('Continue to Second Step &raquo;'); ?>" />
</h2>
</form>

<?php
	break;
	case 2:

// Fill in the data we gathered
$site_title = stripslashes($_POST['site_title']);
$admin_email = stripslashes($_POST['admin_email']);
// check e-mail address
if (empty($admin_email)) {
	die (__("<strong>ERROR</strong>: please type your e-mail address"));
} else if (!is_email($admin_email)) {
	die (__("<strong>ERROR</strong>: the e-mail address isn't correct"));
} else if (empty($site_title)) {
		die (__("<strong>ERROR</strong>: please enter a site title"));
}
	
?>
<h1><?php _e('Second Step'); ?></h1>
<p><?php _e('Now we&#8217;re going to create the database tables and fill them with some default data.'); ?></p>


<?php
flush();

// Set everything up
wp_cache_flush();

$install_queries = explode( ';', $wp_queries );

foreach( $install_queries as $query )
	$wpdb->query($query);

//populate_options();
$optiondomain = 'system';
populate_system_options();
//populate_roles();

//$wp_rewrite->flush_rules();

// Set up admin user
//NO $password = uniqid(microtime());
$password = substr(md5(uniqid(microtime())), 0, 6);
$display_name_array = explode('@', $admin_email);
$display_name = $display_name_array[0];
wp_create_user( 'admin', $password, $admin_email);

$wpdb->query("UPDATE $wpdb->users SET user_admin='1' WHERE ID='1'");
//$optiondomain = 'blog';

$message_headers = "From: \"".$site_title.'"<'.$admin_email.'>';
$message = sprintf(__("Your new Lyceum installation has been successfully set up at:

%1\$s

You can log in to the administrator account with the following information:

Username: admin
Password: %2\$s

You can access the system administration control panel at the following url:

%1\$s"."system-admin/settings.php

We hope you enjoy your new blogging services system. Thanks!

--The Lyceum Team
http://lyceum.ibiblio.org/
"), $siteurl, $password);

@wp_mail($admin_email, __('New Lyceum Installation'), $message, $message_headers);

//upgrade_all();
$admin_email = mysql_real_escape_string($admin_email);

$wpdb->query("INSERT INTO `$wpdb->options` (`option_name`,`option_value`, `option_domain`) VALUES ('site_title', '" . mysql_real_escape_string($site_title) . "', 'system') ");
$wpdb->query("UPDATE `$wpdb->options` SET option_value = '" . mysql_real_escape_string($admin_email) . "' WHERE option_name = 'system_admin_email';");

if (!SAVEWPCOMMENTS){
	$commentfilehash = "wp-comments-post_".md5(uniqid(microtime())).".php";
	$wpdb->query("INSERT INTO `$wpdb->options` (`option_name`,`option_value`, `option_domain`) VALUES ('wp-comments-post_hash', '$commentfilehash', 'system') ");
	rename("../wp-comments-post.php", "../".$commentfilehash) or die('<h1>Failed when trying to rename wp-comments-post.php. This is probably due to insufficient permissions.</h1>');
} else
	$wpdb->query("INSERT INTO `$wpdb->options` (`option_name`,`option_value`, `option_domain`) VALUES ('wp-comments-post_hash', 'wp-comments-post.php', 'system') ");
?>

<p><em><?php _e('Finished!'); ?></em></p>
<dl>
<dt><?php _e('Username'); ?></dt>
<dd><code>admin</code></dd>
<dt><?php _e('Password'); ?></dt>
<dd><code><?php echo $password; ?></code></dd>
	<dt><?php _e('Login address'); ?></dt>
<dd><a href="../login">login</a></dd>
<dt>System options control panel:</dt>
<dd><a href="<?php echo $siteurl; ?>system-admin/settings.php"><?php echo $siteurl; ?>system-admin/settings.php</a></dd>
</dl>

<?php
	break;
}
?>
<p id="footer"><?php _e('<a href="http://lyceum.ibiblio.org/">Lyceum</a>: Yes!'); ?></p>
</body>
</html>
